package com.koron.css2.newProjectCenter.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.newProjectCenter.bean.WorkDayBean;
import com.koron.css2.newProjectCenter.mapper.WorkDayMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import com.koron.util.Tools;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

public class WorkDayController implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        WorkDayBean bean = JsonUtils.objectToPojo(req.getData(), WorkDayBean.class);
        if (bean.getTodo() == null || bean.getTodo() == 1) {
            // 分页获取所有工作日信息
            return getList(factory, userInfo, bean);
        } else if (bean.getTodo() == 2) {
            // 批量更新输入的年份的节假日
            return saveBatch(factory, userInfo, bean);
        } else if (bean.getTodo() == 3) {
            // 根据id更新工作日工作信息
            return update(factory, userInfo, bean);
        }
        return null;
    }

    private MessageBean<?> saveBatch(SessionFactory factory, UserInfoBean userInfo, WorkDayBean bean) {
        Integer year = bean.getYear();
        if (year == null)
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "参数错误", null);
        WorkDayMapper mapper = factory.getMapper(WorkDayMapper.class);
        try {
            String result = HttpUtil.get("https://api.haoshenqi.top/holiday?date=" + year);
            List<WorkDayBean> workDays = JSON.parseArray(result, WorkDayBean.class);
            workDays.forEach(workDay -> {
                workDay.setCreateInfo(userInfo);
                workDay.setId(Tools.getObjectId());
            });
            // 先删除当前对应年份的数据
            mapper.deleteByYear(year);
            mapper.insertBatch(workDays);
            return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "保存成功", null);
        } catch (Exception e) {
            logger.error("保存工作日信息失败", e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "保存失败", null);
        }
    }

    private MessageBean<?> getList(SessionFactory factory, UserInfoBean userInfo, WorkDayBean bean) {
        WorkDayMapper mapper = factory.getMapper(WorkDayMapper.class);
        try {
            PageHelper.startPage(bean.getPage(), bean.getPageCount());
            List<WorkDayBean> list = mapper.selectList(bean);
            PageInfo<WorkDayBean> pageInfo = new PageInfo<>(list);
            MessageBean result = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", PageInfo.class);
            result.setData(pageInfo);
            return result;
        } catch (Exception e) {
            logger.error("查询工作日信息失败", e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "查询失败", null);
        }
    }

    private MessageBean<?> update(SessionFactory factory, UserInfoBean userInfo, WorkDayBean bean) {
        if (bean.getId() == null || bean.getStatus() == null)
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "参数错误", null);
        WorkDayMapper mapper = factory.getMapper(WorkDayMapper.class);
        try {
            // 判断早班上班下班时间、午班上下班时间是否均合法（即早班上班时间小于早班下班时间小于午班上班时间小于午班下班时间）
            if (bean.getMorningStart() == null || bean.getMorningEnd() == null || bean.getAfternoonStart() == null || bean.getAfternoonEnd() == null) {
                bean.setMorningStart(null);
                bean.setMorningEnd(null);
                bean.setAfternoonStart(null);
                bean.setAfternoonEnd(null);
            } else if (!(bean.getMorningStart().before(bean.getMorningEnd()) && bean.getMorningEnd().before(bean.getAfternoonStart()) && bean.getAfternoonStart().before(bean.getAfternoonEnd())))
                return MessageBean.create(Constant.MESSAGE_INT_FAIL, "上班时间参数异常", null);
            bean.setUpdateInfo(userInfo);
            mapper.update(bean);
            return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "更新成功", null);
        } catch (Exception e) {
            logger.error("更新工作日信息失败", e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "更新失败", null);
        }
    }
}
